package cn.lingyangwl.framework.log;

import cn.hutool.http.GlobalInterceptor;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.log.common.LogCons;
import cn.lingyangwl.framework.log.core.customizer.MdcFieldCustomizerConfig;
import cn.lingyangwl.framework.log.core.filter.CustomTraceUser;
import cn.lingyangwl.framework.log.integration.hutoolhttp.LyLogHutoolHttpInterceptor;
import cn.lingyangwl.framework.log.integration.resttemplate.LogRestTemplateInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.TtlMDCAdapter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

import javax.annotation.PostConstruct;

/**
 * @author shenguangyang
 */
@AutoConfiguration
@Import({
        LogRestTemplateInterceptor.class, MdcFieldCustomizerConfig.class, WebLogAspect.class,
        LogRunner.class
})
@EnableConfigurationProperties(LyLogProperties.class)
public class LyLogAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(LyLogAutoConfiguration.class);

    @PostConstruct
    @SuppressWarnings("all")
    public void init() {
        TtlMDCAdapter.init();
        Resp.setTraceIdKey(LogCons.TRACE_ID);

        // hutool
        GlobalInterceptor.INSTANCE.addRequestInterceptor(new LyLogHutoolHttpInterceptor());

        log.info("init {}", this.getClass().getName());
    }



    @Bean
    @ConditionalOnMissingBean(CustomTraceUser.class)
    public CustomTraceUser defaultCustomTraceUser() {
        return () -> null;
    }
}
